package concurrent_util;

import java.util.HashMap;

/**
 * @description:
 * @author: ywk
 * @date: 2020-12-05
 */
public class 同步方法 {
    public static  int race = 0;

    public  void increase() {
        race++;
    }
    static final int tableSizeFor(int cap) {
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= 1000) ? 1000 : n + 1;
    }
    public static void main(String[] args) throws InterruptedException {
        HashMap map = new HashMap(8);
        final 同步方法 test = new 同步方法();
        final 同步方法 test1 = new 同步方法();
        Thread[] threads = new Thread[20];
        for (int i = 0; i < 2; i++) {
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 100; j++) {
                        test.increase();
                    }
                }
            });
            threads[i].start();
        }
        for (int i = 0; i < 2; i++) {
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 100; j++) {
                        test1.increase();
                    }
                }
            });
            threads[i].start();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(race);
            }
        }).start();
        Thread.currentThread().join();
        System.out.println(同步方法.race);
    }
}